package com.gardenguell.salad.util;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import com.gardenguell.salad.R;

/**
 * Created by absurd on 2015/11/20.
 */

public class DialogUtil {

    public static ProgressDialog showProgressDialog(Context context,
                                                    String title, String message) {
        return ProgressDialog.show(context, title, message);
    }

    public interface ConfirmCallback {
        String getPositiveHint();

        void onPositive(DialogInterface dialog);

        String getNegativeHint();

        void onNegative(DialogInterface dialog);
    }

    public static class BaseConfirmCallback implements ConfirmCallback {

        @Override
        public String getPositiveHint() {
            return ResUtil.getString(R.string.ok);
        }

        @Override
        public void onPositive(DialogInterface dialog) {
        }

        @Override
        public String getNegativeHint() {
            return ResUtil.getString(R.string.cancel);
        }

        @Override
        public void onNegative(DialogInterface dialog) {
        }

    }

    public static AlertDialog showConfirm(Context context, CharSequence title,
                                          CharSequence message, final ConfirmCallback callback) {
        if (context == null) {
            return null;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        OnClickListener ln = new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == DialogInterface.BUTTON_POSITIVE) {
                    callback.onPositive(dialog);
                } else if (which == DialogInterface.BUTTON_NEGATIVE) {
                    callback.onNegative(dialog);
                }
            }
        };
        return builder.setTitle(title).setMessage(message)
                .setPositiveButton(callback.getPositiveHint(), ln)
                .setNegativeButton(callback.getNegativeHint(), ln).show();
    }

    public static AlertDialog showDIYConfirm(Context context, CharSequence title,
                                             CharSequence message, final ConfirmCallback callback,
                                             String leftText,String rightText) {
        if (context == null) {
            return null;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        OnClickListener ln = new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == DialogInterface.BUTTON_POSITIVE) {
                    callback.onPositive(dialog);
                } else if (which == DialogInterface.BUTTON_NEGATIVE) {
                    callback.onNegative(dialog);
                }
            }
        };
        return builder.setTitle(title).setMessage(message)
                .setPositiveButton(rightText, ln)
                .setNegativeButton(leftText, ln).show();
    }

    @SuppressWarnings("unused")
    public static AlertDialog showSingleConfirm(Context context, CharSequence title,
                                                CharSequence message, final ConfirmCallback callback) {
        if (context == null) {
            return null;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        OnClickListener ln = new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == DialogInterface.BUTTON_POSITIVE) {
                    callback.onPositive(dialog);
                }
            }
        };
        return builder.setTitle(title).setMessage(message)
                .setPositiveButton(callback.getPositiveHint(), ln).show();
    }
    @SuppressWarnings("unused")
    public static Dialog showSingleChoiceDialog(Context context, String title,
                                                String[] items, int checkedItem, OnClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        return builder.setTitle(title)
                .setSingleChoiceItems(items, checkedItem, listener).show();
    }



    public static Dialog showItemsDialog(Context context, String title,
                                         String[] items, OnClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        return builder.setTitle(title).setItems(items, listener).show();
    }
}

